package com.wnhz.work.yz.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.CommodityListBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.xutils.x;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class CommodityListActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_list);
        initData();
        initView();
        upLoad();
    }

    private List<CommodityListBean.InfoEntity> infos = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new Hashtable<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        //  Toast.makeText(CommodityListActivity.this, "" + applyId, Toast.LENGTH_SHORT).show();
        map.put("apply_id", applyId);

        XUtil.Post(Url.UCENTER_GETTEPIGOODSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                Log.e("------UCENTER_IST", result);
                CommodityListBean tepi = gson.fromJson(result, CommodityListBean.class);
                if ("1".equals(tepi.getResult())) {
                    infos = tepi.getInfo();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                h.sendEmptyMessage(1);
            }
        });
    }

    Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            setData();


        }
    };

    private void setData() {

        recycler.setAdapter(new Myadapter());
        /**
         *        Log.e("---22222", infos.size() + "");
         x.image().bind(holder.getImageView(R.id.logo_pic), infos.get(position).getLogo_pic());
         holder.setTextView(R.id.package_name, infos.get(position).getPackage_name());
         holder.setTextView(R.id.nums2, "×" + infos.get(position).getNums());
         holder.setTextView(R.id.tc_classname, infos.get(position).getTc_classname());
         holder.setTextView(R.id.room_number_color, "房间号：" + infos.get(position).getRoom_number() + "   配色：" + infos.get(position).getColor());
         holder.setTextView(R.id.package_money, "￥" + infos.get(position).getPackage_money());
         */
    }

    class Myadapter extends BaseAdapter {
        public Myadapter() {

        }

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {
            return infos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = View.inflate(CommodityListActivity.this, R.layout.commodity_item, null);
                holder.imageView = (ImageView) convertView.findViewById(R.id.logo_pic);
                holder.package_name = (TextView) convertView.findViewById(R.id.package_name);
                holder.nums2 = (TextView) convertView.findViewById(R.id.nums2);
                holder.tc_classname = (TextView) convertView.findViewById(R.id.tc_classname);
                holder.room_number_color = (TextView) convertView.findViewById(R.id.room_number_color);
                holder.package_money = (TextView) convertView.findViewById(R.id.package_money);

                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.package_name.setText(infos.get(position).getPackage_name());
            holder.nums2.setText(infos.get(position).getNums());
            holder.tc_classname.setText(infos.get(position).getTc_classname());
            holder.room_number_color.setText("房间号：" + infos.get(position).getRoom_number() + "   配色：" + infos.get(position).getColor());
            holder.package_money.setText("￥" + infos.get(position).getPackage_money());
            x.image().bind(holder.imageView, infos.get(position).getLogo_pic());
            return convertView;
        }

        class Holder {
            ImageView imageView;
            TextView package_name, nums2, tc_classname, room_number_color, package_money;
        }
    }

    private ListView recycler;

    private void initView() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("商品清单");
        recycler = (ListView) findViewById(R.id.recycler222);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private String applyId;

    private void initData() {
        applyId = getIntent().getStringExtra("applyId");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
